package com.apinoo.users.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="users_groups")
@AssociationOverrides({
	@AssociationOverride(name = "pk.user", 
		joinColumns = @JoinColumn(name = "id_user")),
	@AssociationOverride(name = "pk.group", 
		joinColumns = @JoinColumn(name = "id_group")) })
public class UserGroup implements Serializable {

	private UserGroupId pk = new UserGroupId();
	private User createdBy;
	private Date createdDate;
	
	@EmbeddedId
	public UserGroupId getPk() {
		return pk;
	}
	
	public void setPk(UserGroupId pk) {
		this.pk = pk;
	}
	
	@Transient
	public User getUser() {
		return getPk().getUser();
	}
	
	public void setUser(User user) {
		getPk().setUser(user);
	}
	
	@Transient
	public Group getGroup() {
		return getPk().getGroup();
	}
	
	public void setGroup(Group group) {
		getPk().setGroup(group);
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="created_by", nullable = false)
	public User getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="created_date",nullable = false)
	public Date getCreatedDate() {
		return createdDate;
	}
	
	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	public boolean equals(Object o) {
		if ( this == o) return true;
		if ( o == null || getClass() != o.getClass()) return false;
		
		UserGroup that = (UserGroup) o;
		
		if ( getPk() != null ? !getPk().equals(that.getPk()) : that.getPk() != null)
			return false;
		
		return true;	
		
	}
	
	public int hashCode() {
		return ( getPk() != null ? getPk().hashCode() : 0);
	}
	
}
